<?php


namespace app\mobile\service\base;


use app\admin\model\base\Paper;
use app\admin\model\base\Practise;
use app\admin\model\base\PractiseSubject;
use app\admin\model\base\StudentPaper;
use app\admin\model\base\StudentPractiseSubject;
use app\admin\model\StudentPractise;
use app\BaseService;
use think\response\Json;

class IndexService extends BaseService
{
    /**
     * 首页大厅数据
     * @return Json
     */
    public function index($params): Json
    {
        //获取试卷
        $list = Paper::where('is_del','=',0)
            ->where('is_release','=',1)
            ->field('student,id')
            ->select()->toArray();
        $arr = [];
        $arr[] = 999999999;
        if(!empty($list)){
            foreach ($list as $value){
                if(is_string($value['student'])){
                    $a = json_decode($value['student']);
                    if(in_array(MOBILE_USER_ID,$a)){
                        $arr[] = $value['id'];
                    }
                }
            }
        }
        $paperList = Paper::where('is_del','=',0)
            ->with(['paper_subjects','special'])
            ->where('id','in',implode(',',$arr))
            ->where('is_release','=',1)
//            ->where('start_time','<',date('Y-m-d H:i:s',time()))
//            ->where('end_time','>',date('Y-m-d H:i:s',time()))
            ->order('end_time','asc')
            ->limit(5)
            ->select()->toArray();
        $lists = StudentPaper::where('student_id','=',MOBILE_USER_ID)->with(['student_paper_subject'])->select()->toArray();
        $idss = [];
        if(!empty($lists)){
            foreach ($lists as $listsss){
                $idss[$listsss['paper_id']] = $listsss;
            }
        }
        $paperListArr = [];
        if(!empty($paperList)){
            foreach ($paperList as $item){
                $subject_num = count($item['paper_subjects']);
                $subject_score = 0;
                if(!empty($item['paper_subjects'])){
                    foreach ($item['paper_subjects'] as $subject){
                        $subject_score+= $subject['weighting'];
                    }
                }
                $status = 0;
                $student_score = '--';
                $student_subject_num = '--';
                if(isset($idss[$item['id']])){
                    if($idss[$item['id']]['status'] == 1){
                        $student_subject_num = 0;
                        foreach ($idss[$item['id']]['student_paper_subject'] as $value){
                            if($value['status'] == 1){
                                $student_subject_num = $student_subject_num + 1;
                            }
                        }

                    }
                    if($idss[$item['id']]['scoring'] == 1){
                        $status = 3;
                        $student_score = $idss[$item['id']]['score'];
                    }else{
                        if($idss[$item['id']]['status'] == 0){
                            $status = 1;
                        }else{
                            $status = 2;
                        }
                    }
                }
                $paperListArr[] = [
                    'name'=>$item['name'],
                    'start_time'=>$item['start_time'],
                    'end_time'=>$item['end_time'],
                    'subject_num'=>$subject_num,
                    'subject_score'=>$subject_score,
                    'img_url'=>$item['img_url'],
                    'paper_id'=>$item['id'],
                    'special_name'=>$item['special']['name'] ?? '',
                    'status'=>$status,
                    'student_score'=>$student_score,
                    'student_subject_num'=>$student_subject_num,
                ];
            }
        }

        //获取练习
        $practiseList = StudentPractise::where('is_del','=',0)
            ->with(['practise'])
            ->where('student_id','=',MOBILE_USER_ID)
//            ->where('start_time','<',date('Y-m-d H:i:s',time()))
//            ->where('end_time','>',date('Y-m-d H:i:s',time()))
            ->order('end_time','desc')
            ->limit(5)
            ->select()->toArray();
        $practiseListArr = [];
        if(!empty($practiseList)){
            foreach ($practiseList as $item){
                //查询练习学生练习的数据
                $subject_score = 0;
                $lists = PractiseSubject::where('practise_id','=',$item['practise_id'])->select()->toArray();
                if(!empty($lists)){
                    foreach ($lists as $listss){
                        $subject_score+= $listss['weighting'];
                    }
                }
                $subject_num = count($lists);
                $use_subject_num = StudentPractiseSubject::where('student_practise_id','=',$item['id'])->where('status','=',1)->count();
                $practiseListArr[] = [
                    'name'=>$item['practise']['name'],
                    'desc'=>$item['practise']['desc'],
                    'time'=>$item['practise']['time'],
//                    'end_time'=>$item['end_time'],
                    'subject_num'=>$subject_num,
                    'subject_score'=>$subject_score,
                    'student_subject_num'=>$use_subject_num,
                    'img_url'=>$item['practise']['img_url'],
                    'practise_id'=>$item['practise']['id'],
                    'id'=>$item['id'],
                    'student_score'=>$item['score'],
                    'special_name'=>$item['practise']['special']['name'] ?? '',
                ];
            }
        }
        //获取用户今日参加练习的时间
        $todayTime =  StudentPractise::where('student_id','=',MOBILE_USER_ID)->whereDay('end_time')->sum('take_time') ?? 0;
        return jsonReturn(1, ['paper'=>$paperListArr,'practise'=>$practiseListArr,'today_time'=>$todayTime]);
    }
}